<?php
/**
 * mssql驱动
 * 
 * 部分方法未实现
 *
 * @package util.db.drivers.pdo
 * @since 0.2
 */
class IDbPdoMssql extends IAbstractDbPdo {
	public function connect() {
		parent::connect("odbc:Driver={SQL Server};Server={$this->config['host']};Database={$this->config['dbname']};");
	}
	
	public function getPdoType() {
		return "mssql";
	}
	
	public function getTableList() {
		$sql = "SELECT name FROM sysobjects WHERE type='U' ORDER BY name";
		$stmt = $this->query($sql);
		return $stmt->fetchAll(IDb::FETCH_COLUMN, 0);
	}
	
	/**
	 * Enter description here...
	 * 
		 $type;
		 $column;
		 $maxLength = 0;
		 $notNull = false;
		 $primaryKey = false;
		 $autoIncrement = false;
		 $binary = false;
		 $unsigned = false;
		 $hasDefault = false;
		 $defaultValue;
	 *
	 * 此处参考Zend::Db的实现，具体见 http://framework.zend.com
	 * 
	 * @param unknown_type $name
	 * @return unknown
	 * @see IDarField
	 * @todo 需要添加除TYPE_STR,TYPE_FLOAT等的其他类型
	 */
	public function describeTable($name) {
       	$sql = "exec sp_columns @table_name = " . $this->quoteKeyword($name);
        $stmt = $this->query($sql);
        $result = $stmt->fetchAll(IDb::FETCH_NUM); 
        
        $table_owner = 1;
        $table_name  = 2;
        $column_name = 3;
        $type_name   = 5;
        $precision   = 6;
        $length      = 7;
        $scale       = 8;
        $nullable    = 10;
        $column_def  = 12;
        $column_position = 16;
       	
        //owner
        $sql = "exec sp_pkeys @table_name=" . $this->quoteKeyword($name) . ", @table_owner=" . $this->quoteKeyword($result[0][1]);
        $stmt = $this->prepare($sql);
        $stmt->execute();
   	 	$primaryKeysResult = $stmt->fetchAll(IDb::FETCH_NUM);
        $primaryKeyColumn = array();
        $pkey_column_name = 3;
        $pkey_key_seq = 4;
        foreach ($primaryKeysResult as $pkeysRow) {
            $primaryKeyColumn[$pkeysRow[$pkey_column_name]] = $pkeysRow[$pkey_key_seq];
        }

        $desc = array();
        $p = 1;
        foreach ($result as $key => $row) {
            $identity = false;
            $words = explode(' ', $row[$type_name], 2);
            if (isset($words[0])) {
                $type = $words[0];
                if (isset($words[1])) {
                    $identity = (bool) preg_match("/identity/", $words[1]);
                }
            }

            $isPrimary = array_key_exists($row[$column_name], $primaryKeyColumn);
            if ($isPrimary) {
                $primaryPosition = $primaryKeyColumn[$row[$column_name]];
            } else {
                $primaryPosition = null;
            }

            $desc[] = array(
                "column"      => $row[$column_name],
                "defaultValue"          => $row[$column_def],
                "notNull"         => !((bool)$row[$nullable]),
                "maxLength"           => $row[$length],
                "primaryKey"          => $isPrimary
            );
        }
        return $desc;
	}	
	
	
	public function limit($sql, $count, $offset = 0) {
		if ($count + $offset > 0) {
			$sql = preg_replace("/^SELECT\s/i", "SELECT TOP " . ($count + $offset) . " ", $sql);
		}
		return $sql;
	}
	
	public function quoteKeyword($keyword) {
		return $keyword;
	}
}


?>